'''
给你一个字符串数组，请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。

字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
'''

'''
输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
'''


class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        codeG = {}
        for s in strs:
            code = self.encode(s)
            if code not in codeG:
                codeG[code] = []
            codeG[code].append(s)
        
        res = []

        for g in codeG.values():
            res.append(g)
        return res

    


    #利用每个字符的出现次数进行编码
    def encode(self,s):
        count = [0] * 26
        for c in s:
            d = ord(c) - ord('a')
            count[d] += 1
        return str(count)